/******************************************************************************
copyright(c)cguage.com
source code can be free to use and publish, but please keep header comments, thx

author:  cguage
contact: tangcraig@gmail.com
desc:    missing
date:    unknown

*******************************************************************************/
#pragma once
#include <functional>
#include <tuple>
#include <cassert>
#include <boost\coroutine\all.hpp>
#include <boost\date_time\posix_time\posix_time.hpp>
#include "Timer.h"

namespace CggUtil {

typedef bool (*CorUtilCB)(void);
typedef std::tuple<int, CorUtilCB> CorArgType;
typedef boost::coroutines::coroutine<CorArgType> CorType;
extern bool __corSleepCB(void* params, int& interval);

#define COR_BEGIN                                                       \
    auto __cor = new CorType::pull_type([=](CorType::push_type& __cor) {

#define COR_END(timer)                                                  \
    __cor(make_tuple(-1, nullptr));                                     \
    });                                                                 \
    timer.addTask(__corSleepCB, 1, __cor); 

#define COR_SLEEP(time)                                                 \
    assert(time > 0);                                                   \
    __cor(make_tuple(time, nullptr));                                   \
    __cor(make_tuple(1, nullptr));

#define COR_SLEEP_UNTIL(interv, cond)                                   \
    assert(interv > 0);                                                 \
    __cor(make_tuple(interv, []()->bool {cond}));                       \
    __cor(make_tuple(1, nullptr));

} // namespace CggUtil